/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.service.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.ruipengkj.bean.TEmployee;
import com.ruipengkj.bean.TOutBill;
import com.ruipengkj.commons.base.query.BaseQuery;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.dao.TOutBillDao;
import com.ruipengkj.service.TOutBillService;
/**
 * 说明：
 * 单据商品说明Service层接口实现
 * @Author：李智深
 * @Date：2012-03-16
 */
@Service
public class TOutBillServiceImpl implements TOutBillService{

	@Resource
	private TOutBillDao TOutBillDao;//DAO层
	//查询所有
	@Override
	public List<TOutBill> findService(TOutBill entity) throws Exception {
		String hql = "select o from TOutBill o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOutBillDao.findDao(hql, whereHQL.toString(), param, orderby);
	}

	//根据条件翻页查询
	@Override
	public PageModel<TOutBill> getScrollService(int firstResult,
			int maxResult, TOutBill entity) throws Exception {
		String hql = "select o from TOutBill o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillID()+"%");
			}
//			if(entity.getStoreid()!=null&&!entity.getStoreid().equals("")){
//				whereHQL.append("storeid").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
//				param.add("%"+entity.getStoreid()+"%");
//			}
			if(entity.getReasonType()!= 0){
				whereHQL.append("ReasonType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getReasonType()+"%");
			}
			if(entity.getAUser()!=null&&!entity.getAUser().equals("")){
				whereHQL.append("AUser").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getAUser()+"%");
			}
			if(entity.getOperator()!=null&&!entity.getOperator().equals("")){
				whereHQL.append("Operator").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getOperator()+"%");
			}
			if(entity.getStatus()!=0){
				whereHQL.append("Status").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getStatus()+"%");
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOutBillDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}

	//根据条件翻页查询
	@Override
	public PageModel<TOutBill> getScrollService(int firstResult,
			int maxResult, TOutBill entity, List<TEmployee> tEmployeeList) throws Exception {
		String hql = "select o from TOutBill o";
		StringBuffer whereHQL = new StringBuffer();
		List<Object> param = new ArrayList<Object>();
		if (entity != null) {
			if(entity.getBillID()!=null&&!entity.getBillID().equals("")){
				whereHQL.append("BillID").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getBillID()+"%");
			}
			if(entity.getReasonType()!= 0){
				whereHQL.append("ReasonType").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getReasonType()+"%");
			}
			if(entity.getStoreid()!=null&&!entity.getStoreid().equals("")){
				whereHQL.append("storeid").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getStoreid()+"%");
			}
//			if(entity.getAUser()!=null&&!entity.getAUser().equals("")){
//				whereHQL.append("AUser").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
//				param.add("%"+entity.getAUser()+"%");
//			}
			if (tEmployeeList != null && tEmployeeList.size() != 0) {
				whereHQL.append("(");
				for (int i = tEmployeeList.size() - 1; i >= 0; --i) {
					whereHQL.append("AUser").append(BaseQuery.LIKE).append("?").append(BaseQuery.OR);
					param.add("%"+tEmployeeList.get(i).getEmployeeID()+"%");
				}
				whereHQL.delete(whereHQL.length()-3, whereHQL.length());
				whereHQL.append(") "+BaseQuery.AND);
			}
			if(entity.getOperator()!=null&&!entity.getOperator().equals("")){
				whereHQL.append("Operator").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getOperator()+"%");
			}
			if(entity.getStatus()!=0){
				whereHQL.append("Status").append(BaseQuery.LIKE).append("?").append(BaseQuery.AND);
				param.add("%"+entity.getStatus()+"%");
			}
			//按时间查询
			if(null!=entity.getHappenTime()&&!("").equals(entity.getHappenTime())){
				whereHQL.append("HappenTime>=?").append(BaseQuery.AND);
				param.add(entity.getHappenTime());
			}
			if(null!=entity.getEndTime()&&!("").equals(entity.getEndTime())){
				whereHQL.append("HappenTime<=?").append(BaseQuery.AND);
				param.add(entity.getEndTime());
			}
			if(whereHQL.length()>4)
				whereHQL.delete(whereHQL.length()-5, whereHQL.length());
		}
		LinkedHashMap<String, String> orderby = new LinkedHashMap<String, String>();
		return TOutBillDao.getScrollDao(hql, whereHQL.toString(), param, firstResult, maxResult, orderby);
	}
	//修改
	@Override
	public void modifyService(TOutBill entity) throws Exception {
		TOutBillDao.modifyDao(entity);
		
	}
	//添加
	@Override
	public void saveService(TOutBill entity) throws Exception {
		TOutBillDao.saveDao(entity);
	}
	//根据ID删除
	@Override
	public void deleteService(String id) throws Exception {
		TOutBillDao.deleteDao(id);
		
	}
	//根据ID查询
	@Override
	public TOutBill getByIdService(String id) throws Exception {
		return TOutBillDao.getByIdDao(id);
	}
	//添加或修改
	@Override
	public void saveOrUpdateService(TOutBill entity) throws Exception {
		TOutBillDao.saveOrUpdateDao(entity);
	}
	//添加返回ID
	@Override
	public Serializable saveRetService(TOutBill entity) throws Exception {
		return TOutBillDao.saveRetDao(entity);
	}
	public TOutBillDao getTOutBillDao() {
		return TOutBillDao;
	}

	public void setTOutBillDao(TOutBillDao TOutBillDao) {
		this.TOutBillDao = TOutBillDao;
	}
}
